import requests

host = "http://127.0.0.1:8080"


def gen_cert():
    # 签发证书
    res = requests.post(
        url="{}/lts/brood2/cert/gen-ca".format(host),
        json={
            "ca": {
                "CN": "etcd ca",  # common name
                "expiry": 867240,  # ca有效时间(小时)
                "profiles": [
                    {
                        "profile": "etcd",  # profile的名字
                        "expiry": 867240,  # 签发证书有效时间(小时)
                        "usages": ["signing", "key encipherment", "server auth", "client auth"],  # 签发证书用途
                    }
                ],  # 指定不同签发证书使用场景
            },
            "certs": [
                {
                    "name": "etcd",  # 证书文件名称
                    "CN": "etcd",  # common name；k8s提取该字段值作为User
                    "hosts": [
                        "localhost",
                        "127.0.0.1",
                        "172.22.64.224",
                        # "172.22.69.236",
                        # "172.22.69.237",
                        # "172.22.69.238",
                        # "172.22.76.113",
                        # "10.10.0.1",
                        # "30.2.150.151",
                        # "kubernetes",
                        # "kubernetes.default",
                        # "kubernetes.default.svc",
                        # "kubernetes.default.svc.cluster",
                        # "kubernetes.default.svc.cluster.local",
                    ],  # 授权使用该证书的IP、域名列表,为空表示所有ip、域名都可以使用该证书
                    "O": "system",  # Organization，k8s提取该字段值作为Group
                    "profile": "etcd",  # 指定使用ca证书的哪个profile进行签发，确保上面定义的ca有这个profile
                },
            ],  # 需要签发的证书列表，每个元素都是一组证书的配置
        }
    )
    print(res.text)


def get_cert_info(url):
    res = requests.get(
        url="{}/lts/brood2/cert/cert-info".format(host),
        json={
            "cert-link": url,  # 公钥的链接
        }
    )
    print(res.text)


def gen_kubeconfig():
    # 签发kubeconfig
    res = requests.post(
        url="{}/lts/brood2/cert/gen-kubeconfig".format(host),
        json={
            "default_context": "admin",  # 默认的上下文
            "config_name": "abc.kubeconfig",  # kubeconfig文件名称
            "clusters": [
                {
                    "cluster": "kubernetes1",  # 集群名称
                    "apiserver": "https://172.22.76.113:6443",  # apiserver访问地址
                    "user": "kube-admin",  # 用户名
                    "context": "admin",  # 上下文
                    "ca": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/ca.pem",
                    # ca证书
                    "cert": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/k8s-admin.pem",
                    # 自签证书
                    "cert_key": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/k8s-admin-key.pem"
                    # 自签证书私钥
                },
                {
                    "cluster": "kubernetes2",
                    "apiserver": "https://172.22.76.234:6443",
                    "user": "kube-test",
                    "context": "test",
                    "ca": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/ca.pem",
                    "cert": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/k8s-admin.pem",
                    "cert_key": "http://lujs.cn/lts/brood2-static/4f546f07-b9e9-4370-93e6-ec9aabc1270c/cert/k8s-admin-key.pem"
                }
            ]  # 每个元素都是一个集群的配置

        }
    )
    print(res.text)


def deploy_docker():
    # 部署docker
    res = requests.post(
        url="{}/lts/brood2/deploy/deploy-docker".format(host),
        json={
            "hosts": [
                "192.168.235.101",
                "192.168.235.102",
            ],  # 需要安装的机器
            "application_dir": "/wls/application/",  # 安装目录
            "work_dir": "/wls/docker/",  # 工作目录
            "insecure_registry_list": [
                "brood.com",
                "172.22.70.154:5000",
                "172.22.70.155:5000",
                "harbor.lujs.cn",
                "registry.brood.lujs.cn"
            ],  # 受信任的仓库
            "dns_list": [
                "192.168.235.1",
                # "172.19.38.6",
                # "172.22.79.1",
                # "172.22.79.2"
            ],  # dns 服务地址
            "version": "24",  # docker 版本
        },
    )
    print(res.text)


def deploy_etcd():
    # 部署etcd集群
    res = requests.post(
        url="{}/lts/brood2/deploy/deploy-etcd".format(host),
        json={
            "cluster": [
                {"name": "etcd0", "ip": "192.168.235.100"},
                # {"name": "etcd1", "ip": "172.22.67.98"},
                # {"name": "etcd2", "ip": "172.22.67.99"},
            ],  # 需要安装的集群，必须是奇数
            "proxy_hosts": [
                # "30.2.150.151"
            ],  # 代理节点，即将访问该节点的流量转发到实际的etcd服务的节点，没有就空着
            "application_dir": "/wls/application/",  # 安装目录
            "work_dir": "/wls/etcd/data/",  # 工作目录
            "cert_dir": "/wls/etcd/cert/",  # 证书目录
            "conf_dir": "/wls/etcd/conf/",  # 配置文件目录
            "version": "3.5",  # etcd 版本
        },
    )
    print(res.text)


def deploy_bash_completion():
    # 单独部署k8s自动补全工具
    res = requests.post(
        url="{}/lts/brood2/deploy/deploy-bash-completion".format(host),
        json={
            "nodes": [
                "172.22.67.97",
                "172.22.67.98",
                "172.22.67.99",
            ],
            "application_dir": "/wls/application/",  # 安装目录
            "version": "2.11",  # 版本
        },
    )
    print(res.text)


def deploy_nginx():
    # 部署nginx
    res = requests.post(
        url="{}/lts/brood2/deploy/deploy-nginx".format(host),
        json={
            "nodes": [
                "192.168.235.102"
            ],  # 需要部署的机器
            "application_dir": "/wls/application/",  # 安装目录
            "version": "1.24",
        },
    )
    print(res.text)


def deploy_k8s_lb_with_nginx():
    # 在部署nginx的前提下，部署负载均衡配置
    res = requests.post(
        url="{}/lts/brood2/deploy/deploy-k8s-lb-with-nginx".format(host),
        json={
            "nodes": [
                "172.22.76.115",
            ],  # 需要部署的机器
            "nginx_config_dir": "/wls/application/nginx/conf/up_streams",  # up_stream配置目录
            "lb_port": 6443,  # 监听的端口
            "server_list": [
                "172.22.67.97:6443",
                "172.22.67.98:6443",
                "172.22.67.99:6443",
            ],  # 负载均衡的server
            "conf_name": "kubernetes-lb-1.conf",  # 创建的配置文件名称
            "upstream_name": "k8s-lb-upstream1"  # 配置中up-stream名称
        },
    )
    print(res.text)


def deploy_kubernetes():
    url = "/lts/brood2/deploy/deploy-kubernetes"
    res = requests.post(
        url=host + url,
        json={
            # 单master节点不使用proxy_server参数
            "proxy_server": "172.22.76.115:6443",  # 多master需要先部署apiserver代理,可以是nginx负载均衡或者虚拟vip，或者nginx+虚拟ip
            "cluster_name": "67.97-1.23版本k8s集群",  # 集群名称
            "masters": [
                "192.168.235.100",
                # "172.22.67.98",
                # "172.22.67.99",
            ],  # master节点
            "nodes": [
                # "172.22.65.41",
                # "172.22.64.82",
                # "172.22.65.114",
            ],  # node节点
            "proxy_hosts": [
                # "30.2.150.151"
            ],  # 代理节点，通过代理节点访问k8s集群，没有就空着
            "etcd_clusters": [
                "https://192.168.235.100:2379",
                # "https://172.22.67.98:2379",
                # "https://172.22.67.99:2379",
            ],  # etcd集群
            "etcd_certs": {
                "ca": "http://172.19.208.1:9999/static/b60fd299-2c73-48b2-a8e2-c0e1f6c8345c/cert/ca.pem",
                "cert": "http://172.19.208.1:9999/static/b60fd299-2c73-48b2-a8e2-c0e1f6c8345c/cert/etcd-client.pem",
                "key": "http://172.19.208.1:9999/static/b60fd299-2c73-48b2-a8e2-c0e1f6c8345c/cert/etcd-client-key.pem"
            },  # etcd集群证书链接,通过接口创建etcd集群后，会返回证书链接
            "cluster_cidr": "10.200.0.0/16",  # 集群pod虚拟ip段(cidr)
            "service_cluster_ip_range": "10.0.0.0/16",  # 集群svc虚拟ip段(cidr)
            "service_node_port_range": "30000-32767",  # nodePort的svc可以分配的端口范围
            "cluster_dns_service_ip_list": ["10.0.0.2"],  # 集群dns服务器的svc ip
            "network_interface": "ens33",  # 宿主机网络中对外通信的网络接口名称,如ens160等,确保参数正确，否则calico部署不成功，每个节点都需要确认，保证各个节点接口名称相同
            "calico_mode": "ipip",  # 50节点以下用ipip，50节点以上建议bgp
            "upper_dns_server": "192.168.235.1",  # dns根服务器
            "dns_resolve_list": [
                "172.19.38.3 registry.brood.lujs.cn",
                "172.19.38.3 brood.com",
                "172.19.8.107 lujs.cn",
                "172.19.8.107 ep.lujs.net",
                "172.22.192.189 lubastion.lujs.net",
                "172.19.14.65 maven.lujs.cn",
                "172.19.8.107 gitlab.lujs.cn",
                "172.19.38.7 db.l1.lts",
                "172.19.11.7 db.l2.lts",
                "10.254.142.3 redis.sso.lts",
                "30.94.4.6 bettle.lujs.cn",
                "172.19.46.25 datagw.lujs.cn",
                "172.22.67.245 db.irelease2.lts",
                "172.19.45.13 registry.npm.lu-fe.com",
                "172.168.99.202 kms1.lufax.app",
                "172.22.67.245 db.localbuild.lts",
                "172.22.67.247 db.lucas.lts",
                "172.22.65.175 db.qc.lts",
                "172.23.10.12 app.lucloud.com",
                "172.19.78.100 stg-cdh01.lufax.storage",
                "172.19.78.104 104-cdh-kerb.lufax.storage",
                "172.22.67.245 db.build.lts",
                "172.19.8.107 www.pgyer.com",
                "172.19.8.107 wb.paic.com.cn",
                "172.22.65.121 smartlogin.lu.com",
                "172.19.8.107 chat.lujs.cn",
                "172.19.14.66 db.gitlab.lts",
                "172.168.72.4 jenkins.lujs.cn",
                "172.23.10.12 app.lucloud.com",
                "172.22.129.9 harbor.lujs.cn",
                "172.19.8.107 bs.paic.com.cn",
                "172.19.8.107 lu-arch.lujs.cn",
                "172.19.8.107 PAICDOM-LDAP-BX.service.paic.com.cn",
                "172.19.8.107 bs-cns-app.lujs.cn",
                "172.19.8.107 bs-eog-app.lujs.cn",
                "172.19.8.107 bs-eog-app.qa2prd.cn",
                "172.19.8.107 bs-cns-app.qa2prd.cn",
                "172.19.8.107 harbor227.qa2prd.cn",
                "172.19.8.107 harbor327.qa2prd.cn",
                "172.22.72.69  bs-stgint1.lutest.lujs.cn",
                "172.22.75.30 ssdlc.lujs.net",
                "172.22.243.43 ph-cloud-shenyu-boostrsp-ptr-wgq-padis-ptrstg1.paic.com.cn",
                "172.19.8.107 wb.paic.com.cn",
                "172.19.8.107 wb-stg.paic.com.cn",
                "172.19.8.107 alm-stg.paic.com.cn",
                "172.19.8.107 alm.paic.com.cn",
            ],  # 部署在coredns中的域名解析
            "application_dir": "/wls/application",  # 安装目录
            "data_dir": "/wls/kubernetes/data",  # 工作目录
            "cert_dir": "/wls/kubernetes/cert",  # 证书目录
            "config_dir": "/wls/kubernetes/conf",  # 配置目录
            "audit_dir": "/wls/kubernetes/audit",  # 审计日志目录
            "version": "1.23",  # k8s版本
        }
    )
    print(res.text)


def add_node_to_kubernetes():
    url = "/lts/brood2/deploy/add-node-to-kubernetes"
    res = requests.post(
        url=host + url,
        json={
            "nodes": [
                "192.168.235.101",
                "192.168.235.102",
            ],  # 需要添加的节点
            "cluster_id": 4,
            "application_dir": "/wls/application",  # 安装目录
            "data_dir": "/wls/kubernetes/data",  # 工作目录
            "cert_dir": "/wls/kubernetes/cert",  # 证书目录
            "config_dir": "/wls/kubernetes/conf",  # 配置目录
        }
    )
    print(res.text)


if __name__ == '__main__':
    deploy_nginx()
